<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="MySQL,">










<meta name="description" content="由于现场的线上项目一直单机跑，没有冗余备份，为了防止出现机器挂掉，数据丢失的灾难后果，至少需要对数据库进行备份。一开始设想的很简单，写一个crontab定时任务，每天0点执行备份脚本，备份脚本的内容也很简单： 1mysqldump -q -uxxx -pxxx --add-drop-table --all-databases &amp;gt; /opt/backup/all_database.sql 结果">
<meta name="keywords" content="MySQL">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL数据库备份">
<meta property="og:url" content="http://vosamo.gitee.io/blog/2019/03/28/MySQL数据库备份/index.html">
<meta property="og:site_name" content="VOSAMO">
<meta property="og:description" content="由于现场的线上项目一直单机跑，没有冗余备份，为了防止出现机器挂掉，数据丢失的灾难后果，至少需要对数据库进行备份。一开始设想的很简单，写一个crontab定时任务，每天0点执行备份脚本，备份脚本的内容也很简单： 1mysqldump -q -uxxx -pxxx --add-drop-table --all-databases &amp;gt; /opt/backup/all_database.sql 结果">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-03-28T11:58:35.340Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MySQL数据库备份">
<meta name="twitter:description" content="由于现场的线上项目一直单机跑，没有冗余备份，为了防止出现机器挂掉，数据丢失的灾难后果，至少需要对数据库进行备份。一开始设想的很简单，写一个crontab定时任务，每天0点执行备份脚本，备份脚本的内容也很简单： 1mysqldump -q -uxxx -pxxx --add-drop-table --all-databases &amp;gt; /opt/backup/all_database.sql 结果">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://vosamo.gitee.io/blog/2019/03/28/MySQL数据库备份/">





  <title>MySQL数据库备份 | VOSAMO</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">VOSAMO</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://vosamo.gitee.io/blog/blog/2019/03/28/MySQL数据库备份/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vosamo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="VOSAMO">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">MySQL数据库备份</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-28T09:27:33+08:00">
                2019-03-28
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>由于现场的线上项目一直单机跑，没有冗余备份，为了防止出现机器挂掉，数据丢失的灾难后果，至少需要对数据库进行备份。一开始设想的很简单，写一个crontab定时任务，每天0点执行备份脚本，备份脚本的内容也很简单：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -q -uxxx -pxxx <span class="comment">--add-drop-table --all-databases &gt; /opt/backup/all_database.sql</span></span><br></pre></td></tr></table></figure>
<p>结果，没过几天发现每天上午登录系统都出现无响应假死状态，查看日志发现数据库查询没有响应，再连接数据库执行show processlist一看，发现有大量的数据库sql进程被阻塞，原因是数据库被锁了。原来是因为备份导致了数据库被锁，为什么会出现这种情况呢？因为系统有一些采集程序和告警程序，随时向数据库中写入采集数据和监控告警数据，而且数据量特别大。。。总的数据库大小有几十个G，执行mysqldump备份整个数据库时，导致数据库被锁，后面的sql进程就被阻塞，一直处于“waiting for table level lock”的状态。</p>
<p>找到了问题所在，既然是由于数据库过大导致备份锁库，那么能不能在备份的时候排除一些大表呢？（在我们的项目中，比较大的表都是一些日志表和监控采集表，这些数据都不是必需的）答案是肯定的。接下来，我们就通过排除大表来备份数据库。</p>
<a id="more"></a>
<p>第一步，查询出哪些表比较大</p>
<p>在命令行中执行mysql -uxxx -pxxx连接数据库，然后执行以下sql语句：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">MariaDB [(none)]&gt; use information_schema</span><br><span class="line">MariaDB [(none)]&gt; select table_name,round(data_length/1024/1024,2) as size from tables where table_schema=&apos;数据库名&apos; and table_name like &apos;%pmd_%&apos; order by size desc;</span><br><span class="line">+-----------------------------+----------+</span><br><span class="line">| table_name                  | data     |</span><br><span class="line">+-----------------------------+----------+</span><br><span class="line">| pmd_network_portxxx         | 23201.00 |</span><br><span class="line">| pmd_host_sdrxxx             | 18930.97 |</span><br><span class="line">| pmd_host_nicxxx             | 17166.98 |</span><br><span class="line">| pmd_host_cpuxxxx            | 13588.95 |</span><br><span class="line">| pmd_host_diskxxxx           |  3750.00 |</span><br><span class="line">| pmd_network_port            |  3253.89 |</span><br><span class="line">| pmd_host_sdr                |  2843.95 |</span><br><span class="line">| pmd_host_nic                |  2227.95 |</span><br><span class="line">| pmd_vm_diskxxx              |  2066.02 |</span><br><span class="line">| pmd_host_tempxxxx           |  2039.00 |</span><br><span class="line">| pmd_host_volxxx             |  1997.00 |</span><br><span class="line">| pmd_host_cpu                |  1511.91 |</span><br><span class="line">| pmd_vmxxx                   |  1407.02 |</span><br><span class="line">|      .......                |  ......  |</span><br><span class="line">+-----------------------------+----------+</span><br></pre></td></tr></table></figure>
<p>通过上述sql语句查询出来按照数据表大小降序排列的表名，这几个表的数据加起来有几十个G大小，如果在备份时能将这些表忽略，备份的速度应该能大大加快。那么怎么才能在备份时忽略某些表呢？</p>
<p>第二步，使用<code>--ignore-table</code>忽略某些表</p>
<p>查询mysqldump的帮助文档，可以看到使用<code>--ignore-table</code>参数可以将指定的表忽略掉，具体用法是<code>--ignore-table=数据库名.数据表名</code>，并且该参数可以串联叠加使用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@myCentos ~]# mysqldump --help|grep ignore</span><br><span class="line">                      version 4.1.0 or higher. This option is ignored with</span><br><span class="line">  --ignore-table=name Do not dump the specified table. To specify more than one</span><br><span class="line">                      table to ignore, use the directive multiple times, once</span><br><span class="line">                      --ignore-table=database.table.</span><br><span class="line">  --insert-ignore     Insert rows with INSERT IGNORE.</span><br></pre></td></tr></table></figure>
<p>接下来，我们修改一下备份脚本的内容，忽略我们不想备份的大表：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -q -uxxx -pxxx --add-drop-table mydb  \</span><br><span class="line">--ignore-table=mydb.pmd_network_portxxx   \</span><br><span class="line">--ignore-table=mydb.pmd_host_sdrxxx       \</span><br><span class="line">--ignore-table=mydb.pmd_host_nicxxx       \</span><br><span class="line">--ignore-table=mydb.pmd_host_cpuxxxx      \</span><br><span class="line">--ignore-table=mydb.pmd_host_diskxxxx     \</span><br><span class="line">--ignore-table=mydb.pmd_network_port      \</span><br><span class="line">--ignore-table=mydb.pmd_host_sdr          \</span><br><span class="line">--ignore-table=mydb.pmd_vm_diskxxx        \</span><br><span class="line">--ignore-table=mydb.pmd_host_tempxxxx     \</span><br><span class="line">--ignore-table=mydb.pmd_host_volxxx       \</span><br><span class="line">--ignore-table=mydb.pmd_host_nic          \</span><br><span class="line">--ignore-table=mydb.pmd_vmxxx             \</span><br><span class="line">--ignore-table=mydb.pmd_host_cpu          \</span><br><span class="line">--ignore-table=mydb.pmd_host_fanxxx       \</span><br><span class="line">--ignore-table=mydb.pmd_lb                \</span><br><span class="line">--ignore-table=mydb.pmd_host_disk         \</span><br><span class="line">--ignore-table=mydb.pmd_host              \</span><br><span class="line">--ignore-table=mydb.pmd_host_temp         \</span><br><span class="line">--ignore-table=mydb.pmd_host_vol          \</span><br><span class="line">--ignore-table=mydb.pmd_vm_disk           \</span><br><span class="line">--ignore-table=mydb.pmd_server_disk       \</span><br><span class="line">--ignore-table=mydb.pmd_network           \</span><br><span class="line">--ignore-table=mydb.pmd_vm                \</span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> /opt/backup/icpbak.sql</span></span><br></pre></td></tr></table></figure>
<p>你想忽略另一张表，只需要多加一行–add-drop-table即可。</p>
<p>再次执行备份脚本，很快就运行完成。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/MySQL/" rel="tag"># MySQL</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/02/22/ceph在线修改和查看参数/" rel="next" title="ceph在线修改和查看参数">
                <i class="fa fa-chevron-left"></i> ceph在线修改和查看参数
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/04/12/记录一个Cinder-volume无法删除的错误：ImageBusy-error-raised-while-deleting-rbd-volume/" rel="prev" title="记录一个Cinder volume无法删除的错误：ImageBusy error raised while deleting rbd volume">
                记录一个Cinder volume无法删除的错误：ImageBusy error raised while deleting rbd volume <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.gif" alt="vosamo">
            
              <p class="site-author-name" itemprop="name">vosamo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vosamo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
